import axios from 'axios'
import {message, Modal, notification} from 'ant-design-vue'
import moment from 'moment'
import store from '../store'
import db from 'utils/localstorage'
import { changeDate } from 'utils/common'
moment.locale('zh-cn')

// 统一配置
let FEBS_REQUEST = axios.create({
  // baseURL: 'http://139.159.241.187:9095',
  baseURL: '/api',
  // baseURL: 'http://tea.ezmate.cn/api',
  // baseURL: 'http://192.168.100.176:8080',
  responseType: 'json',
  validateStatus (status) {
    // 200 外的状态码都认定为失败
    return status === 200
  }
})

// 拦截请求
FEBS_REQUEST.interceptors.request.use((config) => {
  // console.log(config)
  let expireTime = store.state.account.expireTime
  let now = moment().format('YYYYMMDDHHmmss')
  // 让token早10秒种过期，提升“请重新登录”弹窗体验
  if (now - expireTime >= -10) {
    Modal.error({
      title: '登录已过期',
      content: '很抱歉，登录已过期，请重新登录',
      okText: '重新登录',
      mask: false,
      onOk: () => {
        return new Promise((resolve, reject) => {
          db.clear()
          location.reload()
          resolve()
        })
      }
    })
  }
  // 有 token就带上
  if (store.state.account.token) {
    config.headers.Authorization = store.state.account.token
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

// 拦截响应
FEBS_REQUEST.interceptors.response.use((res) => {
  let data = res.data
  if (data instanceof Blob) {
    return res
  }
  if (data.status === 1) {
    changeDate(data.data)
    return data
  } else {
    showError({status: data.status, msg: data.msg})
    return Promise.reject(data)
  }
}, (error) => {
  if (error.response) {
    let errorMessage = '系统内部异常，请联系综合办'
    if (error.response.data && error.response.data instanceof Blob) {
      let reader = new FileReader()
      reader.addEventListener('loadend', function () {
        let jsonResult = JSON.parse(reader.result)
        errorMessage = jsonResult.msg
        showError({status: error.response.status, msg: errorMessage})
      })
      reader.readAsText(error.response.data, 'utf-8')
    } else {
      errorMessage = error.response.data === null ? '系统内部异常，请联系综合办' : error.response.data.msg
      showError({status: error.response.status, msg: errorMessage})
    }
  }
  return Promise.reject(error)
})
let showError = (error) => {
  switch (error.status) {
    case 404:
      notification.error({
        message: '系统提示',
        description: '很抱歉，资源未找到',
        duration: 4
      })
      break
    case 403:
      Modal.error({
        title: '登录已过期',
        content: '很抱歉，登录已过期，请重新登录',
        okText: '重新登录',
        mask: false,
        onOk: () => {
          return new Promise((resolve, reject) => {
            db.clear()
            location.reload()
            resolve()
          })
        }
      })
      break
    case 401:
      notification.warn({
        message: '系统提示',
        description: '很抱歉，您无法访问该资源，可能是因为没有相应权限或者登录已失效',
        duration: 4
      })
      this.$router.push('/exception/401')
      break
    default:
      notification.error({
        message: '系统提示',
        description: error.msg,
        duration: 4
      })
      break
  }
}
const request = {
  post (url, params) {
    if (!params) {
      params = {}
    }
    return FEBS_REQUEST.post(url, params, {
      transformRequest: [(params) => {
        let result = ''
        Object.keys(params).forEach((key) => {
          if (!Object.is(params[key], undefined) && !Object.is(params[key], null)) {
              result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
          }
        })
          return result
      }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
  },
    postToDow(url, params) {
        if (!params) {
            params = {}
        }
        return FEBS_REQUEST.post(url, params, {
            headers: {
                'Content-Type': 'application/json'
            },
            responseType: 'blob',
        }).then((r) => {
            console.dir(r.headers)
            console.dir(r)
            const content = r.data
            const blob = new Blob([content])
            console.log(blob)
            var fileName = '.xlsx'
            if (r.headers['content-disposition']) {
                fileName = decodeURI(r.headers['content-disposition'].split('filename=')[1])
            } else {
                return r
            }
            console.log(fileName)
            if ('download' in document.createElement('a')) {
                const elink = document.createElement('a')
                elink.download = fileName
                elink.style.display = 'none'
                elink.href = URL.createObjectURL(blob)
                document.body.appendChild(elink)
                elink.click()
                URL.revokeObjectURL(elink.href)
                document.body.removeChild(elink)
            } else {
                navigator.msSaveBlob(blob, fileName)
            }
            // return r
        })
    },
    postJson(url, params) {
        if (!params) {
            params = {}
        }
        return FEBS_REQUEST.post(url, params, {
            headers: {
                'Content-Type': 'application/json'
            }
        })
    },
  put (url, params) {
    if (!params) {
      params = {}
    }
    return FEBS_REQUEST.put(url, params, {
      transformRequest: [(params) => {
        let result = ''
        Object.keys(params).forEach((key) => {
          if (!Object.is(params[key], undefined) && !Object.is(params[key], null)) {
            result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
          }
        })
        return result
      }],
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  },
  get (url, params) {
    let _params
    if (Object.is(params, undefined)) {
      _params = ''
    } else {
      _params = '?'
      for (let key in params) {
        if (params.hasOwnProperty(key) && params[key] !== null) {
          _params += `${key}=${params[key]}&`
        }
      }
    }
    return FEBS_REQUEST.get(`${url}${_params}`)
  },
  delete (url, params) {
    let _params
    if (Object.is(params, undefined)) {
      _params = ''
    } else {
      _params = '?'
      for (let key in params) {
        if (params.hasOwnProperty(key) && params[key] !== null) {
          _params += `${key}=${params[key]}&`
        }
      }
    }
    return FEBS_REQUEST.delete(`${url}${_params}`)
  },
  export (url, params = {}) {
    message.loading('导出数据中',0)
    return FEBS_REQUEST.post(url, params,{
      transformRequest: [(params) => {
        let result = ''
        Object.keys(params).forEach((key) => {
          if (!Object.is(params[key], undefined) && !Object.is(params[key], null)) {
            result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
          }
        })
        return result
      }],
      responseType: 'blob',
      timeout:0
    }).then((r) => {
      message.destroy()
      const content = r.data
      const blob = new Blob([content])
      const fileName = decodeURI(r.headers['content-disposition'].replace('filename=', '').replace('attachment;', ''))
      if ('download' in document.createElement('a')) {
        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href)
        document.body.removeChild(elink)
      } else {
        navigator.msSaveBlob(blob, fileName)
      }
    }).catch((r) => {
      message.destroy()
      // message.error('导出失败')
    })
  },
  downloadPdf (url, params, filename) {
    message.loading('文件传输中')
    return FEBS_REQUEST.post(url, params, {
      transformRequest: [(params) => {
        let result = ''
        Object.keys(params).forEach((key) => {
          if (!Object.is(params[key], undefined) && !Object.is(params[key], null)) {
            result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
          }
        })
        return result
      }],
      responseType: 'blob',
      // headers: {
      //   'Content-Type': 'application/pdf'
      // }
    }).then((r) => {
      const content = r.data
      const blob = new Blob([content])
      if ('download' in document.createElement('a')) {
        const elink = document.createElement('a')
        elink.download = filename
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href)
        document.body.removeChild(elink)
      } else {
        navigator.msSaveBlob(blob, filename)
      }
    }).catch((r) => {
      // message.error('下载失败')
    })
  },
  download (url, params, filename) {
    message.loading('文件传输中')
    return FEBS_REQUEST.post(url, params, {
      transformRequest: [(params) => {
        let result = ''
        Object.keys(params).forEach((key) => {
          if (!Object.is(params[key], undefined) && !Object.is(params[key], null)) {
            result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
          }
        })
        return result
      }],
      responseType: 'blob'
    }).then((r) => {
      const content = r.data
      const blob = new Blob([content])
      if ('download' in document.createElement('a')) {
        const elink = document.createElement('a')
        elink.download = filename
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href)
        document.body.removeChild(elink)
      } else {
        navigator.msSaveBlob(blob, filename)
      }
    }).catch((r) => {
      // message.error('下载失败')
    })
  },
  upload (url, params) {
    if (!params) {
      params = {}
    }
    return FEBS_REQUEST.post(url, params, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

export default request
